<?php

namespace app\index\controller;

use think\Request;
use think\Db;
use think\Session;
use Myredis\Myredis;
use phpemail\Email;
use app\index\model\xoUser;
class Registe extends Unlogin
{
    /**
     * 显示注册页面
     * @return \think\response\View
     */
    public function index()
    {
        return view('index', [
            'title' => '注册'
        ]);
    }

    /**
     * 处理注册验证
     * @return array
     */
    public function doRegiste(Request $request)
    {
        //验证数据是否来自post传输方式
        if (!Request::instance()->isPost()) {
            return $this->error('小伙子,是不是迷路了', url('index/index/index'));
        }
        //接收post传过来的数据
        $telCode = Request::instance()->post('telCode');
        $tel = Request::instance()->post('phone');
        $pCode = Myredis::get($tel . 'telCode');
        $pass = Request::instance()->post('pass');
        //判断验证码是否正确
        if ($telCode != $pCode) {
            return $this->error('验证码输入错误,请重试');
        }
        //根据Tel查询数据库判断是否已存在
        $telRes = Db::table('xo_user')->where('tel', $tel)->find();
        if ($telRes > 0) {
            return $this->error('该手机号已注册过,请登录', url('index/login/index'));
        }
        //验证手机号码格式
        if (!preg_match('/^1(3[0-9]|5[189]|8[6789])[0-9]{8}$/', $tel)) {
            return $this->error('手机格式错误', url('index/registe/index'));
        };
        //验证密码格式
        if (!preg_match("/^[a-z\d]{6,12}$/i", $pass)) {
            return $this->error('密码格式不正确', url('index/registe/index'));
        }
        //验证好的数据存入数组
        $data = [
            'tel' => $tel,
            'pwd' => md5($pass),
            'status' => 1
        ];
        //对数据库进行增入
        $res = Db::table('xo_user')->insertGetId($data);
        // 对redis 进行插入数据
        $data['id']=$res;
        //将数据存入redis
        Myredis::hset('userLogin', $tel, $data);
        //判断是否插入成功
        if ($res > 0) {
            //添加成功则往redis中添入相同的数据
            return $this->success('注册成功', url('index/login/index'));
        } else {
            return $this->error('注册失败');
        }

    }

    /**
     * 处理邮箱注册,发送邮箱激活链接
     */
    public function email()
    {
        if(!Request::instance()->isPost()){
            return $this->redirect('error/index');
        }
        $email = Request::instance()->post('email');
        $pwd = Request::instance()->post('pwd');
        //验证邮箱是否存在
        $res  =Db::table('xo_user')->where(['email'=>$email])->find();
        if(!empty($res)){
            return $this->error('邮箱账号已存在', url('index/registe/index'));
        }
        //验证邮箱格式
        if (!preg_match('/^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@([a-zA-Z0-9]+[-.])+([a-z]{2,5})$/ims', $email)) {
            return $this->error('邮箱格式不正确', url('index/registe/index'));
        }
        //验证密码格式
        if (!preg_match("/^[a-z\d]{6,12}$/i", $pwd)) {
            return $this->error('密码格式不正确', url('index/registe/index'));
        }
        $token = md5(time() . mt_rand(1,1000000));
        $data = [
            'email'=>$email,
            'pwd'=>md5($pwd)
        ];
        $a = "<a href= http://www.xxoa.top/indexdoemail?token=".$token.'>点击验证</a>';
        $pemail =  new Email();
        $pemail->sendemail($email,$a);
        $redis = MyRedis::setex($token,300,$data);
        if($redis){
            return $this->success('请前往邮箱完成认证操作',url('index/login/index'));
        }else{
            return $this->error('注册失败,请重试');
        }

    }
    /**
     * 处理qq用户的绑定
     * @return [type] [description]
     */
    public function doqq()
    {
        if (!Request::instance()->isPost()){
            return $this->error('小伙子迷了路',url('index/index/index'));
        }
        //接受数据
        $tel = Request::instance()->post('phone');
        $telCode = Request::instance()->post('telCode');
        $pass = Request::instance()->post('pass');
        $newPass = Request::instance()->post('newPass');
        $openid = Request::instance()->post('openid');
        //验证数据
        $res = Db::table('xo_user')->where('tel',$tel)->find();
        if (!empty($res)){
            return $this->error('手机号码已存在');
        }
        if($telCode!=Myredis::get('telCode')){
            return $this->error('手机验证码不正确');
        }
        if($pass != $newPass){
            return $this->error('两次密码输入不正确');
        }
        // 处理数据
        $data = [
            'pwd'=>md5($pass),
            'tel'=>$tel,
            'openid'=>$openid,
            'regime'=>time(),
            'nickname'=>$tel
        ];
        $res = Db::table('xo_user')->insert($data);
        if($res){
            return $this->success('绑定成功成功','index/login/index');
        }else {
            return $this->error('绑定失败');
        }
    }
    /**
     * 处理邮箱激活
     */
    public function doemail()
    {
        $token = Request::instance()->get('token');
        $data = MyRedis::get($token);
        if ($data){
            $data = MyRedis::get($token);
            $data['nickname']=$data['email'];
            $data['regime']=time();
            $xouser = new xoUser($data);
            $xouser->allowField(true)->save();
            return $this->success('激活成功',url('index/login/index'));
        }else{
            return $this->error('链接已失效请重新注册',url('index/registe/index'));
        }
    }
    /**
     * 处理短信验证
     * @return array|void
     */
    public function domsg(Request $request)
    {
        //判断是否来自 ajax
        if (!Request::instance()->isAjax()) {
            return $this->error('小伙子,是不是迷路了', url('index/index/index'));
        }
        $telNum = Request::instance()->post('phone');
        $num = rand(100000, 999900);
        $res = $this->sendTemplateSMS($telNum, array($num, '5'), "1");
        if ($res == '000000') {
            //短信发送成功将验证码存入redis 等待验证
            Myredis::setex($telNum . 'telCode', 300, $num);
            $data = [
                'status' => true,
                'info' => '发送成功',
            ];
        } elseif ($res == '160038') {
            $data = [
                'status' => false,
                'info' => '发送频繁,歇一会'
            ];
        } else {
            $data = [
                'status' => false,
                'info' => '发送失败'
            ];
        }
        return $data;
    }
}
